﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Web.Configuration;

namespace WXYW
{
    public partial class BJXXConfirmClientReveive : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindForms();
            }
        }

        private void BindForms()
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            if (!product.ApplicationFormReference.IsLoaded)
            {
                product.ApplicationFormReference.Load();
            }
            if (!product.ApplicationForm.UserReference.IsLoaded)
            {
                product.ApplicationForm.UserReference.Load();
            }
            if (!product.ProductTypeReference.IsLoaded)
            {
                product.ProductTypeReference.Load();
            }
            if (product.ApplicationForm.ApplicationType.Equals("BB"))
            {
                applyTypeLabel.Text = "备件先行";
            }
            else if (product.ApplicationForm.ApplicationType.Equals("BD"))
            {
                applyTypeLabel.Text = "DOA";
            }
            //RMACodeLabel.Text = product.ApplicationForm.ApplicationCode;
           // runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            contactLabel.Text = product.ApplicationForm.User.ContactPerson;
            companyLabel.Text = product.ApplicationForm.User.FullName;
            phoneNumberLabel.Text = product.ApplicationForm.User.UserPhone;
            cellPhoneLabel.Text = product.ApplicationForm.User.UserMobilePhone;
            faxLabel.Text = product.ApplicationForm.User.UserFax;
            addressLabel.Text = product.ApplicationForm.User.UserAddress;
            emailLabel.Text = product.ApplicationForm.User.UserEmail;
            shxxLbl.Text = product.ApplicationForm.BJXXSHAddress;
            Label1.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            Label2.Text = product.DealWithDateTime.Value.ToString("yyyy-MM-dd");
            
            UserService userservice = new UserService();
            if (product.ApplicationForm.AgentID.HasValue)
            {
                User agent = userservice.GetUserByID(product.ApplicationForm.AgentID.Value);
                Label3.Text = agent.ContactPerson;
                Label4.Text = agent.UserPhone;
                hfAgentEmail.Value = agent.UserEmail;
            }

            //
            runningNumberLabel.Text = product.ApplicationForm.RunningNumber;
            //clientNameLabel.Text = product.ApplicationForm.User.FullName;
            spcodeLabel.Text = product.SPCode;
            productTypeLabel.Text = product.ProductType.ProductTypeName;
            productSerivalNumberLabel.Text = product.ProductSerialNumber;
            versionNumberLabel.Text = product.ProductVersionNumber;
            // dealWithTimeLabel.Text = product.DealWithDateTime.HasValue ? product.DealWithDateTime.Value.ToString("yyyy-MM-dd") : "";
            //  applyTimeLabel.Text = product.ApplicationForm.ApplicationDateTime.Value.ToString("yyyy-MM-dd");
            //  buyTimeLabel.Text = product.ProductPurchaseDateTime.Value.ToString("yyyy-MM-dd");
            bjxxstatusLabel.Text = product.Status;
            // reasonLabel.Text = product.ReasonOfNotRepair;
            repairTupeLabel.Text = product.ProductRepairType;
            requireDisPLabel.Text = product.BJSQNeedComment;
            sendToClientLabel.Text = product.BJXXSendToClient.HasValue ? product.BJXXSendToClient.Value.ToString("yyyy-MM-dd") : "";
            approveCommentLabel.Text = product.BJXXApproveComment;

            producttypeTextBox.Text = product.ReplaceProductType;
            productserialnumberTextBox.Text = product.ReplaceProductSerialNumber;
            productVersionTextBox.Text = product.ReplaceProductVersion;
            sendRemarkTextBox.Text = product.Remark;
            shxxLbl.Text = product.ApplicationForm.BJXXSHAddress;
            agentLabel.Text = product.ApplicationForm.ProviderName;
            clientReceiveTimeTextBox.Text = DateTime.Now.ToString("yyyy-MM-dd");
            // bind last send product information
            if (product.ApplicationForm.ParentBJXXNO.HasValue)
            {
                ApplicationService appservice = new ApplicationService();
                ApplicationForm parentForm = appservice.GetApplicationFormByID(product.ApplicationForm.ParentBJXXNO.Value);
                if (parentForm != null && !parentForm.ApplicationProduct.IsLoaded)
                {
                    parentForm.ApplicationProduct.Load();
                }

                if (parentForm.ApplicationProduct.Count > 0)
                {
                    lastproductserialnumberTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductSerialNumber;
                    lastproducttypeTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductType;
                    lastproductVersionTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().ReplaceProductVersion;
                    lastsendRemarkTextBox.Text = parentForm.ApplicationProduct.FirstOrDefault().Remark;
                }

                reSendPanel.Visible = true;
            }

        }

        protected void sendLinkButton_Click(object sender, EventArgs e)
        {
            ApplicationProductService service = new ApplicationProductService();
            ApplicationProduct product = service.GetApplicationProductByID(long.Parse(Request.QueryString[Constants.QUERYSTR_PRODUCT_ID]));
            product.Status = "备件收货完毕待收回备件确认";
            product.BJXXClientReceive = Convert.ToDateTime(clientReceiveTimeTextBox.Text);
            product.BJXXClientReceiveMark = markTextBox.Text;
            service.UpdateProduct(product);

            string body = WebConfigurationManager.AppSettings["BJXXClientReceiveProduct"];
            string emailBody = string.Format(body, contactLabel.Text, product.SPCode);

            EmailService.SendEmail(emailLabel.Text, "备件先行客户收货确认", emailBody);

            if (!string.IsNullOrEmpty(hfAgentEmail.Value))
            {
                EmailService.SendEmail(hfAgentEmail.Value, "备件先行客户收货确认", emailBody);
            }


            ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('备件客户收货完毕待备件收回确认');location.href='BJXXListConfirmClientReceived.aspx';", true);
        }

    }
}
